<?php
/**********************
**adddevice.php
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once('private/shared/errors.inc');
require_once('private/shared/form.inc');
require_once('private/bottom.inc');
require_once('private/dba.inc');
require_once('private/device.inc');
require_once('private/intellistrip.inc');
require_once('private/top.inc');


function addDeviceCheck(&$failureReason)
{
	//We may get some of these for default values, but require lots of values for an actual add
	if( !isset($_REQUEST[FORM_INTELLISTRIP_ID]) || !isset($_REQUEST[FORM_INTELLISTRIP_PORT_ID]) )
	{
		return TT_OK;
	}
	$intellistripId = $_REQUEST[FORM_INTELLISTRIP_ID];
	$intellistripPort = $_REQUEST[FORM_INTELLISTRIP_PORT_ID];
	
	//Create our intellistrip object
	$intellistrip = new Intellistrip($intellistripId);

	//Note that form values will often times still get sent, abeit blank
	//We also need a way to add a device though
	//A type and a name allows us to create a new device
	//We may also just want to add an existing device
	//TODO: prevent multiple assignment
	if( isset($_REQUEST[FORM_DEVICE_ID]) && $_REQUEST[FORM_DEVICE_ID] != '' )
	{
		$deviceId = $_REQUEST[FORM_DEVICE_ID];
		$device = new Device($deviceId);
	}
	//If it looks like a full add request was attempted, start processing it 
	//so that error reporting can be done even if its not complete
	else if( (isset($_REQUEST[FORM_DEVICE_TYPE]) && isset($_REQUEST[FORM_DEVICE_NAME]))
				&& ($_REQUEST[FORM_DEVICE_TYPE] != '' || $_REQUEST[FORM_DEVICE_NAME] != '') )
	{
		$type = $_REQUEST[FORM_DEVICE_TYPE];
		$name = $_REQUEST[FORM_DEVICE_NAME];
		if( $type == '' || $name == '' )
		{
			$failureReason = "Please enter a type and a name";
			return TT_GENERAL;
		}
		
		$rc = Device::createNewDevice($device, array('type' => $type, 'name' => $name));
		if( TT_FAILED($rc) )
		{
			$failureReason = "There was a problem adding the device to the system: " . Device::getLastError();
			return TT_GENERAL;
		}
		$device = new Device($device);
	}
	//An unknown request, ignore it 
	else
	{
		return TT_OK;
	}
	
	if( TT_FAILED($intellistrip->setDevice($device, $intellistripPort)) )
	{
		$failureReason = "Couldn't set device: " . Intellistrip::getLastError();
		return TT_GENERAL;
	}

	//At this point, we have successfully added a device to a port, whether it involved a change or whatever

	//Redirect to main page now, user can click on new port to add there if they want to add more
	$URL = "main.php";
    header("Location: $URL");
	die("You should be directed momentarily.");
}

function newDeviceFormPart(&$formStr)
{
	$formStr = "";

	//New device section
	$formStr .= "<B>New device:</B><BR /> <BR />";
	//Name
	$formStr .= "Device Name:  <INPUT name=\"" . FORM_DEVICE_NAME . "\" type=\"text\" size=\"20\" maxlength=\"64\" />";

	$formStr .= "
		<BR />
		<BR />
		";
	
	//Device types select box
	$formStr .= "Type of Device:  <select name=\"" . FORM_DEVICE_TYPE . "\">";
	$rc = Device::getDeviceTypes($deviceTypes);
	if( TT_FAILED($rc) )
	{
		echo "Could not get device types<BR />";
		return TT_GENERAL;
	}
	foreach( $deviceTypes as $key => $value )
	{
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $value[0] . "\">" . $value[1] . "</OPTION>";
	}
	$formStr .= "</SELECT>";
	$formStr .= "
		&nbsp;&nbsp;&nbsp;&nbsp;
		<A href=\"editdevicetypes.php\">Edit device types</a>
		<BR />
		<BR />
		";
		
	return TT_OK;
}

function existingDeviceFormPart(&$formStr, $intellistrip, $portID)
{
	$formStr = "";
	
	$formStr .= "<B>Existing device:</B><BR /> <BR />";
	//Device name select box
	$formStr .= "
		Existing device:  
		<SELECT name=\"deviceId\">
		";
	$rc = Device::getDevices($devices);
	if( TT_FAILED($rc) )
	{
		echo "Failed to get devices!<BR>\n";
		return TT_FAILED;
	}
	
	//An invalid default device if we don't have one
	$defaultDeviceId = -1;
	if( TT_SUCCEEDED($intellistrip->getDevice($device, $portID)) )
	{
		if( TT_SUCCEEDED($device->getDeviceId($defaultDeviceId)) )
		{
			//Done, we have the default device ID
		}
	}

	//The "don't use an existing device" field, leave it blank
	$formStr .= "<OPTION value=\"\" selected></OPTION>";
	//Make a select entry for each device
	foreach( $devices as $key => $value )
	{
		$id = $value;
		$device = new Device($id);
		if( TT_FAILED($device->getName($name)) )
		{
			echo "Couldn't get name<BR>\n";
			return TT_FAILED;
		}
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $id . "\"";
	 	//Select as default if this is the one that brought us to this page
	 	if( $id == $defaultDeviceId )
	 	{
	 		$formStr .= " selected";
	 	}
	 	$formStr .= ">" . $name;
		$formStr .= "</OPTION>\n";
	}
	$formStr .= "</SELECT>";

	return TT_OK;
}

function intellistripFormPart(&$formStr, $intellistripIdIn, $portIdIn)
{
	$formStr = "";

	$formStr .= "<B>Intellistrip selection</B><BR><BR>\n";
	//Intellistrip select box
	$formStr .= "Intellistrip:  <select name=\"" . FORM_INTELLISTRIP_ID . "\">";
	$rc = Intellistrip::getIntellistrips($intellistrips);
	if( TT_FAILED($rc) )
	{
		echo "Failed to get intellistrips!<BR />\n";
		return $rc;
	}
	if( sizeof($intellistrips) == 0 )
	{
		echo "You must add an intellistrip before adding devices to one<BR />\n";
		//Thins went okay, we just didn't do the "normal" case
		return TT_OK;
	}
	foreach( $intellistrips as $key => $value )
	{
		$intellistrip = $value;
		if( TT_FAILED($intellistrip->getIntellistripID($stripId)) )
		{
			echo "Failed to get intellistrip name!<BR />\n";
			return $rc;
		}
		if( TT_FAILED($intellistrip->getName($name)) )
		{
			echo "Failed to get intellistrip name!<BR />\n";
			return $rc;
		}
		//Make default if we passed this in
		if( $stripId == $intellistripIdIn )
		{
			$selectedStr = " selected";  
		}
		else
		{
			$selectedStr = "";
		}
		
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $stripId . "\"" . $selectedStr . ">" . $name . "</OPTION>";
	}
	$formStr .= "
		</SELECT>
		<BR />
		";

	//Intellistrip port select box
	$formStr .= "Port: <SELECT name=\"" . FORM_INTELLISTRIP_PORT_ID . "\">";
	for( $i = 0; $i < INTELLISTRIP_NUMBER_PORTS; $i = $i + 1 )
	{
		$portId = $i + INTELLISTRIP_PORT_BASE;
		//Make default if we passed this in
		if( $portId == $portIdIn )
		{
			$selectedStr = " selected";
		}
		else
		{
			$selectedStr = "";
		}
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $portId . "\"". $selectedStr . ">" . $portId . "</OPTION>";
	}
	$formStr .= "</SELECT>";

	return TT_OK;
}

function addDeviceForm($intellistripIdIn, $portIdIn)
{
	$intellistrip = new Intellistrip($intellistripIdIn);
	$formStr = "";
	
	$formStr .= "
		Please either enter information to create a new device or select an existing device.
		If the existing device is attached to a port, it will be moved to the new port.
		If selected port has a device on it, it will be removed from that port.
		<BR>
		<BR>
		";
		
	//Begin form
	$formStr .= "<FORM name=\"input\" action=\"adddevice.php\" method=\"POST\">";

	if( TT_FAILED(newDeviceFormPart($formStrNewDevice)) )
	{
		return TT_GENERAL;
	}
	$formStr .= $formStrNewDevice;

	//Existing device section
	if( TT_FAILED(existingDeviceFormPart($formStrExistingDevice, $intellistrip, $portIdIn)) )
	{
		return TT_GENERAL;
	}
	$formStr .= $formStrExistingDevice;
	
	//Big space between device and intellistrip information
	$formStr .= "
		<BR />
		<BR />
		";
		
	if( TT_FAILED(intellistripFormPart($formStrIntellistrip, $intellistripIdIn, $portIdIn)) )
	{
		return TT_GENERAL;
	}
	$formStr .= $formStrIntellistrip;


	$formStr .= "
		<BR />
		<BR />
		<BR />
		";
	
	$formStr .= "
		<INPUT name=\"Submit\" type=\"submit\" value=\"Submit\" />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<A href=\"index.php\">Cancel</A>
		</FORM>
		";
		
	$formStr .= "
		<SCRIPT type=\"text/javascript\" language=\"JavaScript\">
		document.forms['input'].elements['" . FORM_DEVICE_NAME . "'].focus();
		</SCRIPT>
		";

	//Successfully created, display the form
	echo $formStr;
	return TT_OK;
}

function displayPage()
{
	$rc = addDeviceCheck($addFailureReason);

	top("Add New Device");
	
	if( TT_FAILED($rc) )
	{
		echo "
			Failed to add device: " . $addFailureReason . "
			<BR>
			<BR>
			";
	}
	
	//These values are set by pages like main.php when adding a new device
	if( isset($_REQUEST[FORM_INTELLISTRIP_ID]) )
	{
		$intellistripId = $_REQUEST[FORM_INTELLISTRIP_ID];
	}
	else
	{
		$intellistripId = null;
	}
	if( isset($_REQUEST[FORM_INTELLISTRIP_PORT_ID]) )
	{
		$portId = $_REQUEST[FORM_INTELLISTRIP_PORT_ID];
	}
	else
	{
		$portId = null;
	}
	
	addDeviceForm($intellistripId, $portId);

	bottom();
}

displayPage();

?>
